<?php

/**
 * File Description
 * 
 * Kao: PHP Web Framework <http://kao.byhuy.com/>.
 * Copyright 2008, Huy Nguyen <http://www.byhuy.com/>.
 * 
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 * 
 */


class Kao_Request
{
    
    /**
     * Url params
     * @var array
     */
    protected $_params = array();
    
    /**
     * Controller Name
     */
    protected $_controllerName = null;
    
    /**
     * HTTP Method
     */
    protected $_httpMethod = null;
    
    /**
     * Request URL
     */
    protected $_requestUrl = null;
    
   
    /**
     * Constructor
     * 
     * @param string $requestUrl Request URL
     * @param string $controllerName Controller name
     * @param string $httpMethod HTTP method
     * @param array $params URL params
     */
    public function __construct($requestUrl, $controllerName, $httpMethod, $params)
    {
        $this->_requestUrl     = $requestUrl;
        $this->_controllerName = $controllerName;
        $this->_httpMethod     = $httpMethod;
        $this->_params         = $params;
    }
    
    
    /**
     * Sets request URL
     * 
     * @param string $url
     * @return Kao_Request
     */
    public function setRequestUrl($url)
    {
        $this->_requestUrl = $url;
        return $this;
    }
    
    /**
     * Return request URL
     * 
     * @return string
     */
    public function getRequestUrl()
    {
        return $this->_requestUrl;
    }
    
    /**
     * Sets controller name
     * 
     * @param string $name
     * @return Kao_Request
     */
    public function setControllerName($name)
    {
        $this->_controllerName = $name;
        return $this;
    }
    
    /**
     * Return controller name
     * 
     * @return string
     */
    public function getControllerName()
    {
        return $this->_controllerName;
    }
    
    /**
     * Sets HTTP method
     * 
     * @param string $method
     * @return Kao_Request
     */
    public function setHttpMethod($method)
    {
        $this->_httpMethod = $method;
        return $this;
    }
    
    /**
     * Returns HTTP method
     * 
     * @return string
     */
    public function getHttpMethod()
    {
        return $this->_httpMethod;
    }
    
    /**
     * Returns a param
     * 
     * @param mixed $name Name or key number of a param
     * @param mixed $default Default value if the param doesn't exist
     * @return string
     */
    public function getParam($name, $default = null)
    {
        if (isset($this->_params[$name]))
        {
            return $this->_params[$name];
        }
        return $default;
    }
    
    /**
     * Get a GET param
     * 
     * @param string $key
     * @param mixed $default
     * @return mixed 
     */
    public function getGet($key = full, $default = null)
    {
       if (null === $key)
        {
            return $_GET;
        }
        return ((isset($_GET[$key])) ? $_GET[$key] : $default); 
    }
    
    /**
     * Get a POST param
     * 
     * @param string $key
     * @param mixed $default
     * @return mixed 
     */
    public function getPost($key = null, $default = null)
    {
        if (null === $key)
        {
            return $_POST;
        }
        return ((isset($_POST[$key])) ? $_POST[$key] : $default);
    }
    
    /**
     * Get uploaded file information
     * 
     * @param string $key
     * @return array
     */
    public function getFile($key)
    {
        if (null === $key)
        {
            return $_FILES;
        }
        return ((isset($_FILES[$key])) ? $_FILES[$key] : false);
    }
    
    /**
     * Return a cookie jar
     * 
     * @param string $key
     * @param string $default
     * @return mixed
     */
    public function getCookie($key = null, $default = null)
    {
        if (null === $key)
        {
            return $_COOKIE;
        }
        return ((isset($_COOKIE[$key])) ? $_COOKIE[$key] : $default);
    }
    
    /**
     * Get raw request body
     * 
     * @return string
     */
    public function getRawBody()
    {
        return file_get_contents('php://input');
    }
    
    /**
     * Get value of a member of $_SERVER or get from apache_request_headers().
     * 
     * Learned from Zend Framework ( Zend_Controller_Request_Http)
     */
    
    public function getHeader($header)
    {
        if (empty($header))
        {
            return false;
        }
        
        $phpServerKey = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
        
        if (isset($_SERVER[$phpServerKey]))
        {
            return $_SERVER[$phpServerKey];
        }
        
        if (function_exists('apache_request_headers')) 
        {
            $headers = apache_request_headers();
            
            if (!empty($headers[$header])) 
            {
                return $headers[$header];
            }
        }
        
        return false;
    }
}


